<template>
  <el-dialog width="50%" title="修改订单数据" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <el-form class="ruleForm" :model="form" ref="ruleForm" label-width="100px">
      <el-form-item v-if="form.num == 2" label="租赁方式" prop="pact_type">
        <el-radio-group v-model="form.pact_type">
          <el-radio v-for="(val, key) in initData.pact_type" :key="key" :label="key">{{ val }}</el-radio>
        </el-radio-group>
        <p class="type_tip">
          <template v-if="form.pact_type == 1">
            <span>固定租期的芝麻信用授权可扣款金额=商品套餐押金+总租金</span>
            <br />商品设置固定租期后，承租人须待租赁期满后才能退还租赁物。固定租期无法使用花呗分期功能。
          </template>
          <template v-if="form.pact_type == 2">
            <span>租完即送的芝麻信用授权可扣款金额=总租金</span>
            <br />商品设置租完即送后，租赁期内承租人不得单方面解除协议提前退还租赁物，承租人须按订单约定按期支付租金。租完即送可使用花呗分期功能。
          </template>
          <template v-if="form.pact_type == 3">
            <span>随租随还的芝麻信用授权可扣款金额=商品套餐押金+总租金</span>
            <br />商品设置随租随还后，承租人可以随时退还租赁物。随租随还无法使用花呗分期功能。
          </template>
        </p>
      </el-form-item>
      <el-form-item v-if="form.num == 2 && form.pact_type == 1" label="每期租金">
        <el-input v-model="form.rent" clearable placeholder="请输入每期租金" />
      </el-form-item>
      <el-form-item v-if="form.num == 2" label="市场价">
        <el-input v-model="form.setmeal_foregift" clearable placeholder="请输入市场价" />
        <p class="type_tip">
          <span>市场价=订单总价值(总租金+买断金)，非必要情况下请勿增加市场价金额，否则会导致代扣金额超出预授权金额造成部分租金/买断金代扣失败。</span>
        </p>
      </el-form-item>
      <el-form-item v-if="form.num == 2" label="间隔天数" prop="intervalDays">
        <div class="flex" style="align-items:center;width:750px">
          <el-radio-group v-model="form.intervalDays">
            <el-radio v-for="(val, key) in initData.intervalDays" :key="key" :label="key">{{ val }}</el-radio>
          </el-radio-group>
          <div class="flex" style="width:460px;margin-left:10px">
            <span>总天数：</span>
            <el-input v-model="form.days" clearable placeholder="请输入总天数" style="width:130px" />
          </div>
        </div>
      </el-form-item>
      <el-form-item v-if="form.num == 2" label="租期" prop="month_total">
        <el-radio-group v-model="form.month_total">
          <el-radio v-for="(val, key) in initData.month_total" :key="key" :label="key">{{ val }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="form.num == 2" label="碎屏险">
        <el-radio-group v-model="form.is_insurance_choose">
          <el-radio v-for="(val, key) in insurance_list" :key="key" :label="key">{{ val }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="form.num == 2" label="联通卡">
        <el-radio-group v-model="form.unicom_id">
          <el-radio v-for="(val, key) in initData.unicom" :key="key" :label="key">{{ val }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="form.num == 2" label="是否参与活动">
        <el-radio-group v-model="form.activity_id">
          <el-radio
            v-for="(val, key) in initData.activity"
            :key="key"
            :label="val.id"
          >{{ val.title }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="form.num == 3" label="买断金">
        <el-input v-model="form.setmeal_price" clearable placeholder="请输入买断金" />
      </el-form-item>
      <el-form-item v-if="form.num == 4" label="供应商">
        <el-select v-model="form.shopname" filterable placeholder="请选择供应商" @change="changeSellerId">
          <el-option
            v-for="item in shopname_data"
            :key="item.shopname"
            :label="item.shopname"
            :value="item.member_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.num == 1" label="分配供应商">
        <el-select v-model="form.member_id" filterable placeholder="请选择供应商" multiple>
          <el-option
            v-for="item in shopname_data"
            :key="item.shopname"
            :label="item.shopname"
            :value="item.member_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.num == 5" label="市场价">
        <el-input v-model="form.setmeal_foregift_apply" clearable placeholder="输入活动后市场价" />
        <p class="type_tip">活动前市场价：{{ form.setmeal_foregift }}</p>
      </el-form-item>
      <el-form-item v-if="form.num == 6" label="推送商户">
        <el-select v-model="form.pushshop" filterable placeholder="请选择供应商" @change="pushShop">
          <el-option
            v-for="item in pushShopList"
            :key="item.name"
            :label="item.name"
            :value="item.shopId"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.num == 7" label="门店">
        <el-select
          v-model="form.s_name"
          filterable
          clearable
          placeholder="请选择门店"
          @change="chooseMd"
        >
          <el-option
            v-for="item in mdDicList"
            :key="item.id"
            :label="item.s_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOrderData, editOrderData, getShopUser, getLeaseRent, setOrderSync } from '@/api/order'
import { shopList } from '@/api/user'
export default {
  // props: ['editItem'],
  props: {
    editItem: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      form: {
        orderId: '',
        pact_type: '',
        month_total: '',
        setmeal_price: '',
        setmeal_foregift_apply: '',
        shopname: '',
        seller_id: '', // 供应商商户ID
        rent: '', // 每期租金
        setmeal_foregift: '', //市场价
        activity_id: '', //活动ID
        member_id: [], //分配供应商ID
        num: '', // 1 租赁方式  2 每期租金 3 买断金 4 供应商
        pushshop: '',
        is_insurance_choose: '',
        unicom_id: '',
        days: '',
        intervalDays: '',
        s_name: '',
      },
      initData: {
        pact_type: [], // 租赁方式
        month_total: [], //租期
        activity: [], //是否参加活动
        setmeal_price: '', // 买断金
        shopname: '', // 供应商
        rent: '', // 每期租金
        setmeal_foregift: '', //市场价
        setmeal_foregift_apply: '', //更新后的预授权金额
        is_insurance_choose: '',
        unicom: ['139套餐', '209套餐', '279套餐', '不参与联通卡套餐'],
        intervalDays: { 7: '7天', 10: '10天', 15: '15天' },
      },
      shopname_data: [], // 供应商列表
      dialogVisible: false,
      pushShopList: [
        {
          shopId: 1,
          name: '都来租',
        },
        {
          shopId: 2,
          name: '万品租',
        },
      ],
      insurance_list: ['一年碎屏险', '两年碎屏险', '三年碎屏险', '不参与碎屏险'],
      originActivityId: '',
      mdDicList: [],
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.init()
        this.getData()
      } else {
        this.originActivityId = 0
      }
    },
    // editItem(val) {
    //   if (val.num == 7) {
    //     shopList({ page: '1', pageSize: '10' }).then((res) => {
    //       this.mdDicList = res.data.map((x) => {
    //         return { s_name: x.s_name, id: x.id }
    //       })
    //     })
    //   }
    // },
  },
  mounted() {
    // this.getData()
  },
  methods: {
    async getData() {
      this.initData.setmeal_price = this.form.setmeal_price
      this.initData.shopname = this.form.shopname
      let allRes = await Promise.all([getOrderData(), getShopUser(), getLeaseRent(this.form.orderId)])
      let [res, res1, res2] = allRes
      this.initData.pact_type = res.pact_type
      this.initData.month_total = res.month_total
      this.initData.activity = res.activity
      this.mdDicList = res.shop_list
      this.shopname_data = res1
      this.form.rent = res2.rent
    },
    init() {
      for (const key in this.form) {
        if (this.editItem[key] !== void 0 && this.editItem[key] !== null) {
          if (key === 'activity_id') {
            this.originActivityId = this.editItem[key]
            this.form[key] = this.editItem[key]
          } else {
            this.form[key] = this.editItem[key].toString() || ''
          }
        }
      }
    },
    changeSellerId(item) {
      this.form.seller_id = item
    },
    pushShop(item) {
      console.log(item)
      this.form.pushshop = item
    },
    chooseMd(item) {
      this.form.s_name = item
    },
    save() {
      console.log(this.form)
      console.log(this.initData)
      let isEditActivity = Number(this.form.activity_id) === Number(this.originActivityId)
      const that = this
      this.$confirm('确定修改？', '修改商品', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          if (that.form.num == 6) {
            const data = {
              shopId: that.form.pushshop,
              orderId: that.form.orderId,
            }
            setOrderSync(data).then((res) => {
              this.$message({
                message: '操作成功',
                type: 'success',
              })
              this.dialogVisible = false
            })
          } else {
            let data = {}
            if (!isEditActivity) {
              data = {
                ...that.form,
                num: 6,
              }
            } else {
              if (that.form.num == 7) {
                data = {
                  shop_id: this.form.s_name,
                  num: that.form.num,
                  orderId: that.form.orderId,
                }
              } else {
                data = { ...that.form }
              }
            }
            editOrderData(data).then((res) => {
              this.dialogVisible = false
              this.$emit('getList', true)
            })
          }
        })
        .catch(() => {})
    },
    cancel() {
      this.dialogVisible = false
      this.$refs['ruleForm'].resetFields()
      this.form.is_insurance_choose = ''
      this.form.unicom_id = ''
    },
  },
}
</script>

<style lang="scss" scoped>
.type_tip {
  color: #999999;
  line-height: 22px;

  span {
    color: #ff4146;
  }
}
</style>
